function InstallFunction(obj,functionName)
{
  obj[functionName] = function() { Request(functionName,arguments,'/browse/rpc'); }
}

var server = {};
InstallFunction(server,'getEndeavorClasses');
InstallFunction(server,'getEntityForm');
InstallFunction(server,'getEntityListing');
InstallFunction(server,'newEntity');
InstallFunction(server,'saveEntity');


function addEntity(form_id) {
 var form_element = document.getElementById(form_id);
 for(var i=0;i < form_element.length;i++) {
  var input_element = form_element.elements[i];
  if(input_element.name != 'entity_type') {
    input_element.disabled = false;
	input_element.value = '';
  }
 }
}

function addNewEntityDialog(select_element_id) {
  $('#get-entity-dialog').dialog("close");
  var select_element = document.getElementById(select_element_id);
  var entity_name = select_element.options[select_element.selectedIndex].value;
  server.getEntityForm(entity_name,addNewEntityDialogSuccess);
}

function addNewEntityDialogSuccess(response) {
  var div_form = document.getElementById('id-new-entity-form');
  if (response.length > 0) {
    div_form.innerHTML = response;
	var form_element = document.getElementById('id_add_entity_dialog_form');
	for(var i=0;i < form_element.length;i++) {
      form_element.elements[i].disabled = false;
	}
  } else {
    div_form.innerHTML = "No response from server";
  }
  $('#add-new-entity-dialog').dialog('open'); 
}

function GetEntityListing(entity_name) {
  server.getEntityListing(entity_name,GetEntityListingSuccess);
}

function GetEntityListingSuccess(response) {
  var dialog_results = document.getElementById('id-dialog-entity-results');
  var output_str = '<table>';
  if(response.length > 0) {
	for(var i=0;i < response.length;i++) {
      output_str += '<tr><td><input type="checkbox" name="dialog_entity_key" id="id_entity_' + i + '" value="' + response[i]['key'] +'"></td>';
	  output_str += '<td>' + response[i]['key'] + '</td>';
	  if(response[i]['display_label']) {
	    output_str += '<td>' + response[i]['display_label'] + '</td>';
	  } else if (response[i]['display_form']) {
	    output_str += '<td>' + response[i]['display_form'] + '</td>';
	  } else if(response[i]['value']) {
	    output_str += '<td>' + response[i]['value'] + '</td>';
	  }
	  output_str += '</tr>';
	}
	output_str += '</table>';
	dialog_results.innerHTML = output_str;
  } else {
    dialog_results.innerHTML = 'No entities found in datastore';
  }
}

function populateDisplay(select_element_id) {
  var select_element = document.getElementById(select_element_id);
  var entity_name = select_element.options[select_element.selectedIndex].value;
  GetEntityListing(entity_name);
}

function SaveEntity(form_id)
{
  var form_element = document.getElementById(form_id);
  var form_info = [];
  for(var i=0;i < form_element.length;i++) {
    var record = [];
    var input_element = form_element.elements[i];
	if(input_element.value.length > 0) {
	  record.push(input_element.name);
	  record.push(input_element.value);
	  // alert("IN SAVE ENTITY " + record['name'] + "\nVALUE=" + record['value']);
	  form_info[i]=record;
	}
    input_element.disabled = true;
    //input_element.value = '';
  }
  server.saveEntity(form_info,SaveEntitySuccess);
}

function SaveEntitySuccess(response) {
  if(response.length > 1) {
    alert("RESPONSE IS " + response);
  } else {
    alert("No response from server");
  }

}

function UpdateCallingElement() {
  var saved_element = document.getElementById('id_calling_element');
  var calling_element = document.getElementById(saved_element.value);
  var entities_values = document.getElementsByName('dialog_entity_key');
  //alert("IN UPDATE CALLING ELEMENT" + entities_values.length);
  for(var i=0;i < entities_values.length;i++) {
    var input_chkbox = entities_values[i];
	if(input_chkbox.checked) {
	  calling_element.value += input_chkbox.value + '\n';
	}
  }
  calling_element.style.display = '';
}

function UpdateEndeavorTabs(entity_name) {
  server.getEndeavorClasses(entity_name,UpdateEndeavorTabsSuccess);
}

function UpdateEndeavorTabsSuccess(response) {
  if (response) {
    $('#frbr-endeavor-tabs .ui-tabs-nav li:eq(0)').html('<a href="#endeavor-work-tab">' + response['work_class'] + '</a>');
	$('#frbr-endeavor-tabs .ui-tabs-nav li:eq(1)').html('<a href="#endeavor-expression-tab">' + response['expression_class'] + '</a>');
	$('#frbr-endeavor-tabs .ui-tabs-nav li:eq(2)').html('<a href="#endeavor-manifestation-tab">' + response['manifestation_class'] + '</a>');
 } else {
   alert("No change");
 }
}



